home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / BC_TI.ARJ / TI722.ASC < prev    next >
Text File  |  1992-02-25  |  3KB  |  133 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.   PRODUCT  :  Borland C++                            NUMBER  :  722
  9.   VERSION  :  2.0
  10.        OS  :  DOS
  11.      DATE  :  February 25, 1992                        PAGE  :  1/2
  12.  
  13.     TITLE  :  Using Floating Points in ISR's
  14.  
  15.  
  16.  
  17.  
  18.   QUESTION:
  19.           How do I use floating point within an interrupt service
  20.           routine?
  21.  
  22.   ANSWER:
  23.           The floating point emulator in Turbo C is NOT re-entrant.
  24.           8087 instructions can only be used in interrupt routines
  25.           if the chip is present, and all programs with 8087
  26.           interrupt handlers should be compiled without emulation.
  27.           Within the interrupt handler, the FNSAVE instruction
  28.           should be used to save the state of the 8087 chip into a
  29.           94 byte state record.  After saving the state, an
  30.           implicit FINIT is executed to bring the chip into its
  31.           default state.  In this state, exceptions are masked, so
  32.           divide-by-zeros, overflows, etc. will produce NANs and
  33.           INFs, not run-time errors. It so happens that this is
  34.           exactly what we want (a run-time error during an
  35.           interrupt would completely crash the system), so no FLDCW
  36.           is required to load a different control word. The FNSAVE
  37.           must be executed before the STI that re-enables
  38.           interrupts. Furthermore, an FWAIT must precede STI to
  39.           make sure that the state has been completely saved. The
  40.           FRSTOR must be followed by an FWAIT to ensure that the
  41.           state has been completely reloaded before the state
  42.           variable is removed from the stack.
  43.  
  44.   EXAMPLE:
  45.   #include <stdio.h>
  46.   #include <conio.h>
  47.   #include <dos.h>
  48.  
  49.   double x = 0;
  50.  
  51.   void interrupt (*save)(void);
  52.  
  53.   void interrupt func(void)
  54.   {
  55.     unsigned char state87[94];
  56.  
  57.   __emit__(
  58.     0xDD,0x76,state87,       /* FNSAVE  [BP+<state87] */
  59.     0x9B,                    /* FWAIT                 */
  60.     0xFB);                   /* STI                   */
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.   PRODUCT  :  Borland C++                            NUMBER  :  722
  75.   VERSION  :  2.0
  76.        OS  :  DOS
  77.      DATE  :  February 25, 1992                        PAGE  :  2/2
  78.  
  79.     TITLE  :  Using Floating Points in ISR's
  80.  
  81.  
  82.  
  83.  
  84.     x += 1;
  85.  
  86.   __emit__(
  87.     0x9B,0xDD,0x66,state87,  /* FRSTOR  [BP+<state87] */
  88.     0x9B);                   /* FWAIT                 */
  89.   }
  90.  
  91.   main()
  92.   {
  93.     char ch = 0;
  94.  
  95.     save = getvect(5); /* the <Shift-PrtScr> interrupt. */
  96.     setvect(5, func);
  97.  
  98.     while (ch != 'q')
  99.     {
  100.       cprintf("%f\r\n", x); /* Each time you hit <Shift-PrtScr> */
  101.       if (kbhit())          /* x will get incremented.          */
  102.            ch = getch();
  103.     }
  104.  
  105.     setvect(5, save);  /* restore original vector for INT 5 */
  106.     return(0);
  107.   }
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.